package utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import common.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.utils.URLEncodedUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
//import java.util.Map;
import java.util.stream.Collectors;

import static common.Login.login;

/**
 * @ProjectName: ApiAutotest
 * @Package: utils
 * @ClassName: testfilter
 * @Author: Administrator
 * @Description: 测试
 * @Date: 2021-08-30 10:36
 * @Version: 1.0
 */
public class MyFilter {
  final  static String json="{\"AGE_6\":\"9\",\"ALL_DIA_10\":\"急性肾小球肾炎,内脏移位,贫血,上呼吸道感染\",\"ASTHMA_MSH_1\":\"有\",\"FIRST_DIA_9\":\"急性肾小球肾炎\",\"HOSPITAL_CODE\":\"YS0001\",\"INDEX_NO\":\"1_1683129_YS0001\",\"INDEX_TIME_8\":\"2012-04-23\",\"INPATIENT_NO\":\"1683129\",\"INPATIENT_NO_7\":\"1683129\",\"MEDREC_TYPE_11\":\"住院\",\"NAME\":\"C16063D3B4CA627F0DA28457193BFE86\",\"NAME_4\":\"C16063D3B4CA627F0DA28457193BFE86\",\"ORG_USER_ID\":\"4418293\",\"PATI_ID\":\"24a85650-81d1-4c2d-82bb-0826d3e19129\",\"PATI_ID_3\":\"24a85650-81d1-4c2d-82bb-0826d3e19129\",\"ROWNUM_\":\"2\",\"SEX_5\":\"女\",\"VISIT_DATE_2\":\"2012-04-06\"}";



    /**
     * 从map中查询想要的map项，根据key
     */
    public static Map<String, Object>  parseMapForFilter(Map<String, Object> map, String filters) {
        if (map == null) {
            return null;
        } else {
            map = map.entrySet().stream()
                    .filter((e) -> checkKey(e.getKey(),filters))
                    .collect(Collectors.toMap(
                            (e) -> (String) e.getKey(),
                            (e) -> e.getValue()
                    ));
        }
        return map;
    }

    /**
     * 通过indexof匹配想要查询的字符
     */
    private static boolean checkKey(String key,String filters) {
        if (key.indexOf(filters)>-1){
            return true;
        }else {
            return false;
        }
    }


    /**

     * Map集合模糊匹配

     * @param map map集合

     * @param keyLike 模糊key

     * @return

     */

    public static List getLikeByMap(Map<String,Object> map, String keyLike){
        List list=new ArrayList<>();
        for (Map.Entry<String,Object> entity : map.entrySet()) {
            if(entity.getKey().indexOf(keyLike)>-1){
                list.add( entity.getValue());

            }

        }

        return list;

    }

    /*
    *
    * @Description: Map转URL
     * @param source
    * @return java.lang.String
    * @author gujinle
    * @date 2021-08-30 17:28
    */
    public static String asUrlParams(Map<String, String> source){
        Iterator<String> it = source.keySet().iterator();
        StringBuilder paramStr = new StringBuilder();
        while (it.hasNext()){
            String key = it.next();
            if (StringUtils.isBlank(source.get(key))){
                continue;
            }
            paramStr.append("&").append(key).append("=").append(source.get(key));
        }
        // 去掉第一个&
        return paramStr.substring(1);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
//        Map maps=(Map) JSON.parse(json);
//        List a=getLikeByMap(maps,"AGE_");
//        System.out.println(a.get(0).toString());

        Data data = login();
        Map<String,String > json=new HashMap<>();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("create_id", data.getUserId());
        json.put("authName", "古金乐");
        String a=asUrlParams(json);
String b= URLEncoder.encode("古金乐","UTF-8");

        System.out.println(b);
    }

}
